package org.wanxian.stores.fragment;

import java.util.List;

import org.wanxian.stores.R;
import org.wanxian.stores.activity.ProductInStoresActivity;
import org.wanxian.stores.adapter.StoresAdapter;
import org.wanxian.stores.base.BaseFragment;
import org.wanxian.stores.db.StoresDao;
import org.wanxian.stores.model.Stores;
import org.wanxian.stores.util.CommonUtil;
import org.wanxian.stores.util.ImageCache;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class StoresFragment extends BaseFragment {

	private ListView lvStores;
	private StoresDao storesDao;
	private Stores stores;
	private List<Stores> storesList;
	private StoreAdapter adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_stores, container, false);
		lvStores = (ListView) view.findViewById(R.id.stores);
		storesDao = new StoresDao(getActivity());
		if(storesDao.getCount() > 0) {
			storesList = storesDao.queryAll();
			adapter = new StoreAdapter(storesList, getActivity());
			lvStores.setAdapter(adapter);
			lvStores.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,
						long id) {
					stores = (Stores) lvStores.getItemAtPosition(position);
					Intent intent = new Intent(getActivity(), ProductInStoresActivity.class);
					intent.putExtra("storeId", stores.getId());
					startActivity(intent);
				}
				
			});
		} 
		
		return view;
	}

	@Override
	public void onStart() {
		super.onStart();
		if(storesDao.getCount() > 0) {
			storesList = storesDao.queryAll();
			if(adapter != null) {
				adapter.notifyDataSetChanged();
			} else {
				adapter = new StoreAdapter(storesList, getActivity());
				lvStores.setAdapter(adapter);
			}
			
		}
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	private class StoreAdapter extends StoresAdapter<Stores> {
		public StoreAdapter(List lists, Context context) {
			super(lists, context);
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null) {
				viewHolder = new ViewHolder();
				convertView = View.inflate(getActivity(), R.layout.item_store, null);
				viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
				viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
				viewHolder.tvCore = (TextView) convertView.findViewById(R.id.tv_core);
				viewHolder.tvAddress = (TextView) convertView.findViewById(R.id.tv_address);
				viewHolder.tvDistance = (TextView) convertView.findViewById(R.id.tv_distance);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			Stores store = lists.get(position);
			Bitmap bitmap = ImageCache.getInstance().get(store.getImage());
			if(bitmap == null) {
				viewHolder.ivIcon.setImageBitmap(CommonUtil.getBitmap(store.getImage()));
			}else {
				viewHolder.ivIcon.setImageBitmap(bitmap);
			}
			
			viewHolder.tvName.setText(store.getName());
			viewHolder.tvCore.setText("" + store.getCore());
			viewHolder.tvAddress.setText(store.getAddress());
			viewHolder.tvDistance.setText("" + store.getDistance());
			return convertView;
		}

	}
	
	static class ViewHolder {
		ImageView ivIcon;
		TextView tvName;
		TextView tvCore;
		TextView tvAddress;
		TextView tvDistance;
	}

}
